package com.galaxy.order.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.galaxy.common.mybatis.core.page.PageQuery;
import com.galaxy.common.mybatis.core.page.TableDataInfo;
import com.galaxy.order.domain.OrderDetail;
import com.galaxy.order.domain.bo.OrderDetailBo;
import com.galaxy.order.domain.vo.OrderDetailVo;
import com.galaxy.order.mapper.OrderDetailMapper;
import com.galaxy.order.service.OrderDetailService;
import io.github.linpeilie.Converter;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单详情信息Service业务层处理
 */
@RequiredArgsConstructor
@Service
public class OrderDetailServiceImpl extends ServiceImpl<OrderDetailMapper, OrderDetail> implements OrderDetailService {

    private final OrderDetailMapper orderDetailMapper;
    private final Converter converter;

    @DS("slave")
    @Override
    public TableDataInfo<OrderDetailVo> queryPageList(OrderDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OrderDetail> lqw = buildQueryWrapper(bo);
        var page = orderDetailMapper.selectPage(pageQuery.build(), lqw);
        List<OrderDetailVo> records = page.getRecords().stream()
            .map(orderDetail -> converter.convert(orderDetail, OrderDetailVo.class))
            .collect(Collectors.toList());
        return new TableDataInfo<>(records, page.getTotal());
    }

    @DS("slave")
    @Override
    public List<OrderDetailVo> queryList(OrderDetailBo bo) {
        LambdaQueryWrapper<OrderDetail> lqw = buildQueryWrapper(bo);
        return orderDetailMapper.selectList(lqw).stream()
            .map(orderDetail -> converter.convert(orderDetail, OrderDetailVo.class))
            .collect(Collectors.toList());
    }

    @DS("slave")
    @Override
    public OrderDetail queryById(Long id) {
        return orderDetailMapper.selectById(id);
    }

    @Override
    public Boolean insertByBo(OrderDetailBo bo) {
        OrderDetail add = converter.convert(bo, OrderDetail.class);
        return orderDetailMapper.insert(add) > 0;
    }

    @Override
    public Boolean updateByBo(OrderDetailBo bo) {
        OrderDetail update = converter.convert(bo, OrderDetail.class);
        return orderDetailMapper.updateById(update) > 0;
    }

    @Override
    public Boolean deleteWithValidByIds(List<Long> ids, Boolean isValid) {
        if (isValid) {
            // TODO: Add validation logic here
        }
        return orderDetailMapper.deleteBatchIds(ids) > 0;
    }

    private LambdaQueryWrapper<OrderDetail> buildQueryWrapper(OrderDetailBo bo) {
        LambdaQueryWrapper<OrderDetail> lqw = Wrappers.lambdaQuery();
        lqw.like(bo.getOrderNo() != null, OrderDetail::getOrderNo, bo.getOrderNo());
        lqw.eq(bo.getOrderId() != null, OrderDetail::getOrderId, bo.getOrderId());
        lqw.eq(bo.getProductId() != null, OrderDetail::getProductId, bo.getProductId());
        lqw.like(bo.getProductName() != null, OrderDetail::getProductName, bo.getProductName());
        return lqw;
    }
}
